package com.example.interceptor;

import com.example.utils.CurrentHolder;
import com.example.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/*
@Slf4j
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.获取请求路径
        String requestURI = request.getRequestURI();
        // 2.判断是否是登录请求
        if (requestURI.contains("login") ) {
            return true;
        }
        // 3.解析token
        String token = request.getHeader("token");
        // 4.如果token为空，返回false
        if (token == null || token.isEmpty()) {
            response.setStatus(401);
            return false;
        }
        try {
            Claims claims = JwtUtils.parseJWT(token);
            Integer id = (Integer) claims.get("id");
            CurrentHolder.setCurrentId(id);
        } catch (Exception e) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
*/
